<?php

class RpcApi
{
    protected $key   = 'UserAuth';
	protected $token = 'no token';
	private static $controllerCache;
	public $uid;
	public $time;
	
	public function __construct($uid, $time) {
	    $this->uid   = $uid;
	    $this->time  = $time;
	}

    public function callApi($uid, $time, $token, $hash, $action)
    {
        $this->callApiStart();

        $systemLimit    =   $this->checkSystemLimit();
        if (!empty(AC::$errorNo))
        {
            return  $this->formatData($systemLimit);
        }
        
        // 哈希校验
        $checkHash =   $this->checkHash($hash);
        if (!empty(AC::$errorNo))
        {
            return  $this->formatData($checkHash);
        }
        
        // 判断action
        $do = $this->checkAction($action);
        if (empty($do))
        {
            return $this->formatData($do);
        }
        
        // 组装数据
        $param = $this->formatParam($uid, $action);
        if (empty($param))
        {
            return $this->formatData($param);
        }

        $result =   $this->$do($param);
        $this->callApiEnd();

        return  $this->formatData($action, $result);
    }

    private function writeDiaryBySpider($param)
	{
		$controller  =   $this->newController('DiaryController');
		$result      =   $controller->addDiary($param);
		return  $result;

	}
    private function sendPrizeMsg($param)
	{
		$controller  =   $this->newController('RpcController');
		$result      =   $controller->sendPrizeMsg($param);
		return  $result;
	}
    private function sendCode($param)
	{
		$controller  =   $this->newController('RpcController');
		$result      =   $controller->sendCode($param);
		return  $result;

	}
    private function loginByCode($param)
	{
		$controller  =   $this->newController('RpcController');
		$result      =   $controller->loginByCode($param);
		return  $result;

	}
    // 修改经验值
    private function changeExpLevel($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->changeExpLevel($param);
        return  $result;
    }
    
    // 任务接口
    private function doTask($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->doTask($param);
        return  $result;
    }
    
    // 校验哈希
    private function checkHash ($hash) 
    {
        $hash_new = $this->generateHash();
        if ($hash != $hash_new) {
            return FALSE;
        }
        
        return TRUE;
    }
    
    // 获取用户信息
    private function getUserInfo ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getUserInfo($param);
        return  $result;
    }
    private function getUserService ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getUserService($param);
        return  $result;
    }
    
    private function getUserDetail ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getUserDetail($param);
        return  $result;
    }
    
    // 获取群组信息
    private function getChatGroupInfo ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getChatGroupInfo($param);
        return  $result;
    }
    
    // 获取话题信息
    private function getTopicInfo ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getTopicInfo($param);
        return  $result;
    }
    
    // 获取日记详情
    private function getDiaryInfo ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getDiaryInfo($param);
        return  $result;
    }
    
    // 更新用户流量
    private function updateUserTraffic ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->updateUserTraffic($param);
        return  $result;
    }

    private function checkUserLogin ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->checkUserLogin($param);
        return  $result;
    }

    private function editDiary ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->editDiary($param);
        return  $result;
    }

    private function registerByMobile ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->registerByMobile($param);
        return  $result;
    }

    private function sendCodeByMobile ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->sendCodeByMobile($param);
        return  $result;
    }

    private function modifyMobileLoginPasswd ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->modifyMobileLoginPasswd($param);
        return  $result;
    }

    private function updateLoginPasswd ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->updateLoginPasswd($param);
        return  $result;
    }

    private function checkPasswdIsValid ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->checkPasswdIsValid($param);
        return  $result;
    }

    private function checkMobileCode ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->checkMobileCode($param);
        return  $result;
    }

    private function getUidByOpenUid ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getUidByOpenUid($param);
        return  $result;
    }

    private function imSupport ($param)
    {
        AC::$closeBdMsgpushVersion = 1;
        $controller  =   $this->newController('ImSupportController');
        $result      =   $controller->callApi($param);
        return  $result;
    }

    private function clientLog ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->clientLog($param);
        return  $result;
    }

    private function addVipUser($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->addVipUser($param);
        return  $result;
    }

    private function addPreVipUser($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->addPreVipUser($param);
        return  $result;
    }

    private function reviewDiary($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->reviewDiary($param);
        return  $result;
    }

    private function sendMsgToUser($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->sendMsgToUser($param);
        return  $result;
    }

    private function backupMessage($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->backupMessage($param);
        return  $result;
    }

    private function getEmotionList($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getEmotionList($param);
        return  $result;
    }

    private function sendSms ($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->sendSms($param);
        return  $result;
    }

    private function addCountWeight ($param)
    {
        $controller  =   $this->newController('CountWeightController');
        $result      =   $controller->addCountWeight($param);
        return  $result;
    }

    private function getRewardList($param)
    {
        $controller  =   $this->newController('DiaryController');
        $result      =   $controller->getRewardList($param);
        return  $result;
    }

    //获取用户绑定账号信息
    private function getConnent($param)
    {
        $controller  =   $this->newController('UserController');
        $result      =   $controller->getConnent($param);
        return  $result;
    }

    //获取文章详情
    private function getArticleData($param)
    {
        $controller  =   $this->newController('RpcController');
        $result      =   $controller->getArticleData($param);
        return  $result;
    }


    // 返回do方法名
    private function checkAction ($action)
    {
        $actions = array(
                'changeExpLevel'    =>    'changeExpLevel',
                'doTask'            =>    'doTask'
        );
        
        if (in_array($action, $actions))
        {
            return $actions[$action];
        }
        
        return $action;
    }
    
    // 组装param参数
    private function formatParam ($uid, $action)
    {
        $param = array();
        $request = DOP::request();
        
        if ($action == 'changeExpLevel') 
        {
            $param = array(
                    'uid'    =>    $uid,
                    'exp'    =>    abs(ceil($request['exp']))
            );
        }
        
        if ($action == 'doTask') {
            // 匹配规则
            $rules = array(
                    'user:invite'             =>    'invite',
                    'user:share'              =>    'share',
                    'user:topic:elite'        =>    'topic_elite',
                    'user:freshman:reward'    =>    'freshman_reward',
                    'user:perfect:information'=>    'perfect_information',
                    'user:write'              =>    'daily_write_reward',
                    'user:ability:award'      =>    'ability_monthly_award'
            );
            
            $task = $rules[$request['task']];
            if (empty($task))
            {
                return $param;
            }
            
            $param = array(
                    'uid'   =>  $uid,
                    'task'  =>  $task
            );
        }
        
        if (empty($param)) {
            return $request;
        }
        
        return $param;
    }
    
    // 生成哈希
    private function generateHash()
    {
        return sha1($this->key.$this->uid.$this->token.$this->time);
    }
    
    // 系统维护
    private function checkSystemLimit()
    {
        return  TRUE;
        AC::$errorNo    =   'SA006';
        return  ' 服务器正在维护中！请稍候访问。';
    }
    
    // 程序开始
    private function callApiStart()
    {
        return  NULL;
    }
    
    // 程序结束
    private function callApiEnd()
    {
        return  NULL;
    }
    
    // 组装数据
    private function formatData($do, $result)
    {
        if (empty(AC::$errorNo))    AC::$errorNo    =   NULL;
        elseif ('adminLogin' != $do)
        {
            //$data['param']  =   DOP::request();
        }
    
        $data['resultData'] =   $result;
        $data['serverTime'] =   AC::SERVER_TIME;
        $data['errorNo']    =   AC::$errorNo;
        $data['errorMsg']   =   AC::$errorMsg;
        $data['do']         =   $do;
    
        return  DOP::encodeData($data);
    }/*}}}*/
    
    # new controller    /*{{{*/
    private function newController($controller)
    {
        if (empty($controller))
        {
            AC::$errorNo    =   'SA001';
            return  FALSE;
        }
        if (isset(self::$controllerCache[$controller])  &&  is_resource(self::$controllerCache[$controller]))
        {
            return  self::$controllerCache[$controller];
        }
        self::$controllerCache[$controller] =   new $controller();
        return  self::$controllerCache[$controller];
    }/*}}}*/

}

